package cn.tb.basic.controller;

import cn.tb.basic.dto.LoginDto;
import cn.tb.basic.dto.WechatDto;
import cn.tb.basic.exception.BusinessException;
import cn.tb.basic.service.ILoginService;
import cn.tb.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * 统一登录接口
 */
@RestController
@RequestMapping("/login")
public class LoginController {


    @Autowired
    private ILoginService iLoginservice;

    /**
     * 后端登录接口
     * @param dto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
            HashMap<String, Object> map = iLoginservice.account(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        }
    }

    /**
     * 手机验证码登录
     * @param dto
     * @return
     */
    @PostMapping("/phoneCode")
    public AjaxResult phoneCode(@RequestBody LoginDto dto){
        try {
            HashMap<String, Object> map = iLoginservice.phoneCode(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        }
    }

    /**
     * 微信扫码登录接口
     * @param dto
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatDto dto){
        try {
            return iLoginservice.wechat(dto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        }
    }

    /**
     * 微信绑定接口
     * @param dto
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatDto dto){
        try {
            HashMap<String, Object> map = iLoginservice.binder(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        }
    }

}
